var express = require('express');
var router = express.Router();
var ManagerModel = require('../models/manager');
var ApplicantModel = require('../models/applicant');
router.get('/', function(req, res, next) {
    if (req.cookies.token) {
        ManagerModel.find({ token: req.cookies.token }, function(err, data) {
            if (data.length > 0) {
                ApplicantModel.find(function(err, data) {
                    if (err) {
                        return err;
                    }
                    var applicants = data.map(function(e) {
                        var date = new Date(e['date']);
                        var time = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
                        return { name: e['name'], sex: e['sex'], grade: e['grade'], area: e['area'], department: e['department'], phone: e['phone'], email: e['email'], time: time, isPayed: e['isPayed'] };
                    });
                    res.render('manage', { target: 'manage', applicants: applicants });
                })
            } else {
                res.redirect(302, 'login');
            }
        })
    } else {
        res.redirect(302, 'login');
    };
});
module.exports = router;